<template>
  <div>
    <el-card v-loading="loading" class="aui-card--fill">
      <!-- <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
        <el-button size="small" @click="closeTab()">{{ $t('close') }}</el-button>
      </div> -->
      <el-divider content-position="left">配置对象</el-divider>
      <el-col :span="24">
        <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
          <el-col :span="12">
            <!-- 预警项编码 -->
            <el-form-item label="预警项编码：" prop="eventCode">
              <span>{{dataForm.eventCode}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 预警项名称 -->
            <el-form-item :label="initParams.pushType==='1'?'规则名称：':'预警项名称：'" prop="eventName">
              <!--文本框-->
              <span>{{dataForm.title}}</span>
            </el-form-item>
          </el-col>
        </el-form>
      </el-col>
      <el-col :span="24">
        <el-divider content-position="left">消息通道</el-divider>
        <channelChild ref="channelChild"></channelChild>
      </el-col>
      <el-col :span="24" v-if="initParams.eventClass!=='SYS'">
        <el-divider content-position="left">提醒对象</el-divider>
        <objectChild ref="objectChild"></objectChild>
      </el-col>
    </el-card>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
import mainContent from '@/views/main-content'
import channelChild from './msgBizRule-channel-child'
import objectChild from './msgBizRule-object-child'
var content = mainContent

export default {
  data () {
    return {
      timestamp: this.$route.params.timestamp,
      loading: false,
      initParams: {},
      dataForm: {
        id: this.$route.params.id
      }
    }
  },
  computed: {
    dataRule () {
      return {
      }
    }
  },
  created () {
    this.initParams = this.$route.params
    console.log('rule', this.initParams)
    // 通过路由参数pid, 控制列表请求操作
    this.$nextTick(() => {
      this.$refs['dataForm'].resetFields()
      if (this.$route.params.id) {
        this.dataForm.id = this.$route.params.id
        this.loading = true
        this.getInfo()
      }
    })
  },
  components: {
    channelChild,
    objectChild
  },
  methods: {
    loadRule () {
      let ref = this.$refs.channelChild
      ref.dataForm.bizTypeCode = this.dataForm.id
      ref.getDataList()
    },
    loadObject () {
      let ref = this.$refs.objectChild
      ref.dataForm.ruleId = this.dataForm.id
      ref.getDataList()
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/msg/msgBizRule/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.loadRule()
        this.loadObject()
      }).catch(() => { })
    },
    // 关闭 tab
    closeTab () {
      content.methods.tabRemoveHandle(this.$route.name)
      this.$emit('refreshDataList')
    },
    async submit () {
      let checkFlag = true
      // 检查主表
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          this.$message({
            message: this.$t('prompt.valid'),
            type: 'error',
            duration: 1500
          })
          checkFlag = false
        }
      })
      if (!checkFlag) {
        return
      }
      // 检查子表
      let child = this.$refs.childList
      if (child) {
        await child.fullValidEvent().then(res => {
          if (!res) {
            checkFlag = false
          } else {
            this.dataForm.children = child.getAllData()
            this.dataForm.deltList = child.deltList
          }
        })
      }
      if (!checkFlag) {
        return
      }
      // 立即消息
      if (this.dataForm.pushType === '0') {
        let data = child.getAllData()
        let row = data.length > 0 ? data[0] : {}
        this.dataForm.children = [{
          ...row,
          title: this.dataForm.eventName,
          eventCode: this.dataForm.eventCode,
          bizSort: '*',
          bizType: '*',
          pushType: '0'
        }]
        this.dataForm.deltList = []
      }
      if (this.dataForm.children.length <= 0) {
        this.$message({
          message: this.$t('prompt.children'),
          type: 'error',
          duration: 500
        })
        return false
      }

      // save
      this.$http[!this.dataForm.id ? 'post' : 'put']('/msg/msgBizEvent/', this.dataForm).then(({ data: res }) => {
        if (res.code !== 0) {
          return false
        }
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
            this.closeTab()
          }
        })
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.submit()
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss">
.el-card.child {
  .el-card__body {
    .el-divider--horizontal {
      margin: 10px 0 20px 0;
    }
    .child_buttons {
      margin: 10px 0;
    }
  }
}
</style>
